package com.xiaowen.ssyx.acl.controller;

import com.xiaowen.ssyx.acl.service.PermissionService;
import com.xiaowen.ssyx.common.result.Result;
import com.xiaowen.ssyx.model.acl.Permission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "菜单管理")
@RestController
@RequestMapping("/admin/acl/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;


    @ApiOperation("查询所有菜单")
    @GetMapping
    public Result list() {
        List<Permission> list = permissionService.queryAllPermission();
        return Result.ok(list);
    }


    @ApiOperation("添加菜单")
    @PostMapping("save")
    public Result save(@RequestBody Permission permission) {
        permissionService.save(permission);
        return Result.ok(null);
    }


    @ApiOperation("修改菜单")
    @PutMapping("update")
    public Result update(@RequestBody Permission permission) {
        permissionService.updateById(permission);
        return Result.ok(null);
    }


    @ApiOperation("递归删除菜单")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        permissionService.removeChildById(id);
        return Result.ok(null);
    }

    @ApiOperation("查看角色拥有的权限")
    @GetMapping("/toAssign/{roleId}")
    public Result getAssign(@PathVariable Long roleId){
        List<Permission> rolePermission = permissionService.getRolePermission(roleId);
        return Result.ok(rolePermission);
    }

    @ApiOperation("为角色分配权限")
    @PostMapping("/doAssign")
    public Result doAssignRoles(@RequestParam Long roleId,@RequestParam Long[] permissionId){
        permissionService.saveRolePermission(roleId,permissionId);
        return Result.ok(null);
    }


}
